package org.benjamin.wechat.service.impl;

import java.sql.Timestamp;

import org.benjamin.wechat.domain.entity.Account;
import org.benjamin.wechat.domain.entity.UserInfo;
import org.benjamin.wechat.repository.AccountRepository;
import org.benjamin.wechat.repository.UserInfoRepository;
import org.benjamin.wechat.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserInfoServiceImpl implements UserInfoService {
	
	@Autowired
	private AccountRepository accountRepository;
	
	@Autowired
	private UserInfoRepository userInfoRepository;

	@Override
	public void subscribeUser(String openId,String wechatId) {
		Account account = this.accountRepository.findByWechatId(wechatId);
		UserInfo userInfo = new UserInfo();
		userInfo.setOpenId(openId);
		userInfo.setCreateTime(new Timestamp(System.currentTimeMillis()));
		if(account != null) {
			userInfo.setAccount(account);
		}
		this.userInfoRepository.save(userInfo);
	}

}
